home *** CD-ROM | disk | FTP | other *** search
/ Aminet 5 / Aminet 5 - March 1995.iso / Aminet / gfx / conv / gfx2grob.lha / gfx2grob / src / IffStuff.c < prev    next >
C/C++ Source or Header  |  1994-11-20  |  6KB  |  210 lines

  1. #ifdef AMIGA
  2. /**************************************************************************
  3.  
  4.             Amiga iff.library functions
  5.  
  6.        Copyright (C) 1994 by Alexandros Loghis,  All Rights Reserved
  7.  
  8. **************************************************************************/
  9.  
  10. #include <stdio.h>    /* remove, fopen, fclose, fread, fwrite */
  11. #include <stdlib.h>    /* malloc, free */
  12.  
  13. #include <proto/exec.h>
  14. #include <proto/dos.h>
  15. #include <graphics/gfx.h>
  16.  
  17. #include "gfx2grob.h"
  18. #include "PrintMsg.h"
  19. #include "Convert.h"
  20. #include "iff.h"
  21. #include "IffStuff.h"
  22.  
  23.  
  24. #define CLRPRMSGBI(A) { ClearBI(FLock, IFFBase);    \
  25.             PrintMsg A ;            \
  26.               }
  27. #define CLRPRMSGIB(M) { ClearIB(IffFile, IFFBase);        \
  28.             PrintMsg(M, gv->Options.InputfileName);    \
  29.               }
  30.  
  31.  
  32. static int IffVersion = IFFVERSION;    /* to get address of int */
  33.  
  34.  
  35. char TEMPFILENAME[] = "RAM:gfx2grob.tmp";
  36.  
  37.  
  38. static void ClearBI(BPTR FLock, struct Library *IFFBase);
  39. static MsgTypePtr GetErr(int IffErr);
  40. static void ClearIB(IFFL_HANDLE IffFile, struct Library *IFFBase);
  41.  
  42. /*************************************************************************/
  43.  
  44. static void ClearBI(BPTR FLock, struct Library *IFFBase)
  45. {
  46.   if (FLock) UnLock(FLock);
  47.   if (IFFBase) CloseLibrary(IFFBase);
  48. }
  49.  
  50. /*************************************************************************/
  51.  
  52. extern void ConvertBinToIff(gvType *gv)
  53. {
  54.   register struct Library *IFFBase;
  55.  
  56.   register BPTR FLock;
  57.  
  58.   struct FileInfoBlock *Fib;
  59.  
  60.   LONG FSize;
  61.  
  62.   void *FBuffer;
  63.  
  64.   struct BitMap Bm;
  65.  
  66.  
  67.   if (!(IFFBase = OpenLibrary(IFFNAME, IFFVERSION)))
  68.     PrintMsg(ERR_IFFOPENLIB_SD, IFFNAME, IffVersion);
  69.  
  70.   if (!(FLock = Lock(gv->Options.InputfileName, SHARED_LOCK)))
  71.     CLRPRMSGBI((ERR_IFFLOCK_S, gv->Options.InputfileName));
  72.   if (!(Fib = (struct FileInfoBlock *)malloc(sizeof (struct FileInfoBlock))))
  73.     CLRPRMSGBI((ERR_IFFMEM));
  74.   if (!Examine(FLock, Fib))
  75.     CLRPRMSGBI((ERR_IFFEXAM_S, gv->Options.InputfileName));
  76.  
  77.   UnLock(FLock);
  78.   FLock = NULL;
  79.   if (!(FSize = Fib->fib_Size))
  80.     CLRPRMSGBI((ERR_IFFSIZE_S, gv->Options.InputfileName));
  81.   free(Fib);
  82.  
  83.   if (!(FBuffer = malloc(FSize))) CLRPRMSGBI((ERR_IFFMEM));
  84.   if (!(gv->Inputfile = fopen(gv->Options.InputfileName, "rb")))
  85.     CLRPRMSGBI((ERR_OPEN_S, gv->Options.InputfileName));
  86.   if (FSize != fread(FBuffer, 1, FSize, gv->Inputfile))
  87.     CLRPRMSGBI((ERR_READ_S, gv->Options.InputfileName));
  88.  
  89.   Bm.BytesPerRow = (UWORD) (CALCWIDTH(gv->Options.Width, 16) / 8);
  90.   Bm.Rows     = (UWORD) gv->Options.Height;
  91.   Bm.Flags     = 0;
  92.   Bm.Depth     = 1;
  93.   Bm.pad     = 0;
  94.   Bm.Planes[0]     = FBuffer;
  95.  
  96.   if (!IFFL_SaveBitMap(gv->Options.OutputfileName, &Bm, NULL, 1))
  97.     CLRPRMSGBI((ERR_WRITE_S, gv->Options.OutputfileName));
  98.   gv->Outputfile = NULL;
  99.  
  100.   free(FBuffer);
  101.   CloseLibrary(IFFBase);
  102. }
  103.  
  104. /*************************************************************************/
  105.  
  106. static MsgTypePtr GetErr(int IffErr)
  107. {
  108.   u_char i = 0;
  109.  
  110.   static struct { int IffErr;
  111.           MsgTypePtr Msg;
  112.         } ErrTab[] = { { IFFL_ERROR_OPEN, ERR_OPEN_S },
  113.                    { IFFL_ERROR_READ, ERR_READ_S },
  114.                    { IFFL_ERROR_NOMEM, ERR_IFFMEM },
  115.                    { IFFL_ERROR_NOILBM, ERR_IFFNOILBM_S },
  116.                    { IFFL_ERROR_NOBMHD, ERR_IFFNOBMHD_S },
  117.                    { IFFL_ERROR_NOBODY, ERR_IFFNOBODY_S },
  118.                    { IFFL_ERROR_BADCOMPRESSION,
  119.                  ERR_IFFBADCOMP_S },
  120.                    { 0, ERR_IFFUNKNOWN_S }
  121.                  };
  122.  
  123.  
  124.   do {
  125.     if (IffErr == ErrTab[i].IffErr) break;
  126.   } while (ErrTab[++i].IffErr);
  127.  
  128.   return (ErrTab[i].Msg);
  129. }
  130.  
  131. /*************************************************************************/
  132.  
  133. static void ClearIB(IFFL_HANDLE IffFile, struct Library *IFFBase)
  134. {
  135.   if (IffFile) IFFL_CloseIFF(IffFile);
  136.   if (IFFBase) CloseLibrary(IFFBase);
  137. }
  138.  
  139. /*************************************************************************/
  140.  
  141. extern void IffToBin(gvType *gv)
  142. {
  143.   register struct Library *IFFBase;
  144.  
  145.   register IFFL_HANDLE IffFile = NULL;
  146.  
  147.   LONG IffErr,
  148.        *pBody,
  149.        BodySize;
  150.  
  151.   APTR pDest;
  152.  
  153.   struct IFFL_BMHD *BmHd;
  154.  
  155.  
  156.   if (!(IFFBase = OpenLibrary(IFFNAME, IFFVERSION))) {
  157.     PrintMsg(CHANGEATTR(MSGATT_NOERRTXT | MSGATT_RETURN), ERR_IFFOPENLIB_SD,
  158.          IFFNAME, IffVersion);
  159.     PrintMsg(MSG_ASSUMBIN_S, gv->Options.InputfileName);
  160.     return;
  161.   }
  162.  
  163.   if (EOF == fclose(gv->Inputfile)) CLRPRMSGIB(ERR_CLOSE_S);
  164.   gv->Inputfile = NULL;
  165.   if (!(IffFile = IFFL_OpenIFF(gv->Options.InputfileName, IFFL_MODE_READ))) {
  166.     if ((IffErr = IFFL_IFFError()) == IFFL_ERROR_NOTIFF) {
  167.       CLRPRMSGIB(ERR_IFFNOIFF_S);
  168.       if (!(gv->Inputfile = fopen(gv->Options.InputfileName, "rb")))
  169.     PrintMsg(ERR_OPEN_S, gv->Options.InputfileName);
  170.       return;
  171.     }
  172.     CLRPRMSGIB(GetErr(IffErr));
  173.   }
  174.  
  175.   if(!(BmHd = IFFL_GetBMHD(IffFile))) CLRPRMSGIB(ERR_IFFNOBMHD_S);
  176.   if(BmHd->nPlanes != 1) CLRPRMSGIB(ERR_IFFNO1PL_S);
  177.   gv->Options.Width  = BmHd->w;
  178.   gv->Options.Height = BmHd->h;
  179.  
  180.   if (!(pBody = IFFL_FindChunk(IffFile, ID_BODY)))
  181.     CLRPRMSGIB(ERR_IFFNOBODY_S);
  182.   if (!(BodySize = *++pBody)) CLRPRMSGIB(ERR_IFFSIZE_S);
  183.   pBody++;                    /* now point to data */
  184.  
  185.   gv->Options.InputfileName = TEMPFILENAME;
  186.   if (!(gv->Inputfile = fopen(TEMPFILENAME, "wb")))
  187.     CLRPRMSGIB(ERR_OPEN_S);
  188.   if (BmHd->compression == IFFL_COMPR_NONE) {
  189.     if (BodySize != fwrite((void *) pBody, 1, BodySize, gv->Inputfile))
  190.       CLRPRMSGIB(ERR_WRITE_S);
  191.   }
  192.   else {
  193.     BodySize = (CALCWIDTH(BmHd->w, 16) / 8) * BmHd->h;    /* uncompressed */
  194.     if (!(pDest = (APTR) malloc(BodySize))) CLRPRMSGIB(ERR_IFFMEM);
  195.     if (BodySize != IFFL_DecompressBlock((APTR) pBody, pDest, BodySize,
  196.                      BmHd->compression))
  197.       CLRPRMSGIB(GetErr(IFFL_IFFError()));
  198.     free(pDest);
  199.     if (BodySize != fwrite((void *) pDest, 1, BodySize, gv->Inputfile))
  200.       CLRPRMSGIB(ERR_WRITE_S);
  201.   }
  202.   if (EOF == fclose(gv->Inputfile)) CLRPRMSGIB(ERR_CLOSE_S);
  203.  
  204.   if (!(gv->Inputfile = fopen(TEMPFILENAME, "rb"))) CLRPRMSGIB(ERR_OPEN_S);
  205.  
  206.   IFFL_CloseIFF(IffFile);
  207.   CloseLibrary(IFFBase);
  208. }
  209. #endif /* AMIGA */
  210.